package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Grade;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IGradeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("grade")
public class GradeController {

    @Autowired
    private IGradeService gradeService;

    @RequestMapping("list")
    @ResponseBody
    public List<Grade> list() {
        return gradeService.selectAll();
    }

    @RequestMapping("view")
    public String query(QueryObject qo) {
        return "grade";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult grade(QueryObject qo) {
        return gradeService.query(qo);
    }


    //跟句教室id查看班级
    @RequestMapping("selectByRoomId")
    @ResponseBody
    public List<Grade> selectByRoomId(Long roomId) {
        return gradeService.selectByRoomId(roomId);
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Grade grade) {
        JSONResult result = new JSONResult();
        try {
            gradeService.saveOrUpdate(grade);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Long id) {
        JSONResult result = new JSONResult();
        try {
            gradeService.changeState(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long gradeId) {
        JSONResult result = new JSONResult();
        try {
            Grade grade = gradeService.selectByPrimaryKey(gradeId);
            if (grade.getState()){
                result.setMsg("傻逼不准删!");
                result.setSuccess(false);

            }else {
                gradeService.delete(gradeId);
            }
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("selectByClassroomId")
    @ResponseBody
    public JSONResult selectByClassroomId(Long id) {
        JSONResult result = new JSONResult();
        Grade grade = gradeService.selectByClassroomId(id);
        if(grade != null){
            result.setSuccess(true);
            return result;
        }else {
            result.setSuccess(false);
            return result;
        }
    }
    @RequestMapping("updateTeacher")
    @ResponseBody
    public JSONResult updateTeacher(Long sid,Long tid){
        JSONResult jsonResult=new JSONResult();
        Grade grade = gradeService.selectByPrimaryKey(sid);
        gradeService.updateTeacher(sid, tid);
        return jsonResult;
    }

}
